package com.fanrui.code.family;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.fanrui.code.db.jvmdb.IPersistable;
import com.fanrui.code.entity.FamilyList;
import com.fanrui.code.message.PlayerSession;
import com.fanrui.code.message.PlayerSessions;
import com.fanrui.code.player.Player;
import com.fanrui.code.player.PlayerManager;
import com.fanrui.proto.cmd.FamilyMemberInfoCmd;
import com.fanrui.proto.cmd.PlayersItem;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * 家族中的成员
 * */
public class FamilyMember implements IPersistable<Player> {
	/**
	 * 玩家
	 * */
	private Player player;
	
	/**
	 * 职位id
	 * */
	private FamilyJobEntity familyJob;
	
	/**
	 * 加入时间
	 * */
	private long joinTime;
	
	/**
	 * 俱乐部积分
	 */
	private long clubIncome;
	
	private int clubId;
	
	private Short status;
	
	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public FamilyMember(JsonElement jsonElement) {
		if(jsonElement != null && jsonElement.isJsonObject()){
			JsonObject jsonObject = jsonElement.getAsJsonObject();
			initFamilyJob(jsonObject.get("job").getAsInt());
			joinTime = jsonObject.get("joinTime").getAsLong();
			status = jsonObject.get("status").getAsShort();
			long playerId = jsonObject.get("playerId").getAsLong();
			player =  PlayerManager.getInstance().findOrCreatePlayer(playerId);
		}
	}
	
	public FamilyMember(Player player, boolean isOwer, int clubId) {
		joinTime = new Date().getTime();
		initFamilyJob(isOwer?FamilyJobConfig.OWERJOBID:FamilyJobConfig.NORMALJOBID);
		this.player = player;
		this.clubIncome = 0;
		this.clubId = clubId;
		this.status = 1;
	}
	
	public FamilyMember(FamilyList fl) {
		joinTime = fl.getJointime().getTime();
		initFamilyJob(fl.getJob());
		player =  PlayerManager.getInstance().findOrCreatePlayer(fl.getPlayerId());
		clubIncome = fl.getClubIncome();
		clubId = fl.getClubId();
		status = fl.getStatus();
	}

	private void initFamilyJob(Integer familyJobId) {
		familyJob = FamilyJobConfig.getInstance().createFamilyJobEntity(familyJobId);
	}
	
	
	

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	

	public long getJoinTime() {
		return joinTime;
	}

	public void setJoinTime(long joinTime) {
		this.joinTime = joinTime;
	}
	
	public long getClubIncome() {
		return clubIncome;
	}

	public void setClubIncome(long clubIncome) {
		this.clubIncome = clubIncome;
	}
	
	public void addClubIncome(long add) {
		this.clubIncome += add;
	}

	public FamilyMemberInfoCmd toMemberInfoCmd(){
		FamilyMemberInfoCmd.Builder builder = FamilyMemberInfoCmd.newBuilder();
		builder.setPlayerJob(getJobId());
		builder.setInfo(player.toCmd());
		if (getJobId()==2) {
			builder.setIsGuanli(true);
		}
		if (getStatus()==2) {
			builder.setIsDong(true);
		}
		return builder.build();
	}
	public static Collection<FamilyMemberInfoCmd> toMemberInfoCmds(Collection<FamilyMember> list){
		if(list != null && !list.isEmpty()){
			ArrayList<FamilyMemberInfoCmd> result = new ArrayList<FamilyMemberInfoCmd>();
			for(FamilyMember familyMemebr : list){
				result.add(familyMemebr.toMemberInfoCmd());
			}
			return result;
		}else{
			return Collections.emptyList();
		}
	}
	
	public static FamilyMemberInfoCmd toMineInfoCmd(Collection<FamilyMember> list,Long playerId){
		if(list != null && !list.isEmpty()){
			for(FamilyMember familyMemebr : list){
				if (familyMemebr.getPlayer().getId().equals(playerId)) {
					return familyMemebr.toMemberInfoCmd();
				}
			}
		}
		return null;
	}

	public JsonObject toJson(){
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("playerId", player.getId());
		jsonObject.addProperty("job", getJobId());
		jsonObject.addProperty("joinTime", joinTime);
		jsonObject.addProperty("clubIncome", getClubIncome());
		jsonObject.addProperty("status", getStatus());
		return jsonObject;
	}
	
	public static List<JsonObject> toJsonArray(Collection<FamilyMember> members){
		if(members != null && !members.isEmpty()){
			ArrayList<JsonObject> result = new ArrayList<JsonObject>();
			for(FamilyMember member : members){
				result.add(member.toJson());
			}
			return result;
		}else{
			return Collections.emptyList();
		}
	}
	
	public PlayersItem toClubPlayers() {
		PlayersItem.Builder builder = PlayersItem.newBuilder();
		builder.setNickeName(player.getNickname());
		builder.setIcon(player.getHeadUrl());
		builder.setUserID(player.getId());
		builder.setLevel(getJobId()-1);
		//是否在线
		int isOnline;
		PlayerSession session = PlayerSessions.getPlayerSession(player.getId());
		if(session != null) {
			isOnline = 1;
		}else {
			isOnline = 3;
		}
		if(player.getGamerId() != null) {
			isOnline = 2;
		}
		builder.setGameState(isOnline);
		builder.setIncome(getClubIncome());
//		builder.setIncomeTax(10);
		return builder.build();
	}

	public FamilyJobEntity getFamilyJob() {
		return familyJob;
	}
	
	public void setFamilyJob(FamilyJobEntity familyJob) {
		this.familyJob = familyJob;
	}
	public void setFamilyJob(int jobId) {
		this.familyJob = FamilyJobConfig.getInstance().createFamilyJobEntity(jobId);
	}

	public int getJobId(){
		return familyJob.getJobId();
	}
	
	public void save() {
		FamilyMemberDao.getInstance().save(this);
	}

	@Override
	public void setPersistKey(Player player) {
		this.player = player;
	}

	@Override
	public Player getPersistKey() {
		return player;
	}

	public FamilyList toFamilyList() {
		FamilyList familyList = new FamilyList();
		familyList.setPlayerId(player.getId());
		familyList.setJob(familyJob.getJobId());
		familyList.setJointime(new Date(joinTime));
		familyList.setStatus(status);
		familyList.setClubId(clubId);
		familyList.setClubIncome(clubIncome);
		return familyList;
	}
	
}
